package com.ray;

import java.sql.*;

/**
 * 第一个jdbc程序
 */
public class lesson01 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载驱动, f4进去看一下, 只是为了执行这个Driver类的静态代码块, 在这个过程中注册了一个驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.用户信息和url
        String url = "jdbc:mysql://localhost:3306/jdbcstudy?useSSL=true&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8";
        String username = "root";
        String password = "ray18219";
        //3.连接成功, connection为返回的数据库对象, 有提交回滚等数据库方法
        Connection connection = DriverManager.getConnection(url, username, password);
        //4.执行sql对象, 查看结果
        Statement statement = connection.createStatement();
        String sql = "select * from jdbcstudy.users";
        ResultSet resultSet = statement.executeQuery(sql);
        while(resultSet.next()){
            System.out.print("id=" + resultSet.getObject("id") + "\t");
            System.out.print("name=" + resultSet.getObject("name") + "\t");
            System.out.print("password=" + resultSet.getObject("password") + "\t");
            System.out.print("email=" + resultSet.getObject("email") + "\t");
            System.out.println("birthday=" + resultSet.getObject("birthday"));
        }
        //5.释放连接
        resultSet.close();
        statement.close();
        connection.close();
    }
}
